package com.zuowei.ycy.user.ui.index;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.and.yzy.frame.util.RetrofitUtils;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.adapter.PinJiaAdapter;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.PinJiaInfo;
import com.zuowei.ycy.user.http.Merchant;
import com.zuowei.ycy.ui.BaseFgt;
import com.zuowei.ycy.user.util.AppJsonUtil;
import com.zuowei.ycy.user.util.PtrInitHelper;

import java.util.List;

import butterknife.Bind;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.loadmore.LoadMoreContainer;
import in.srain.cube.views.ptr.loadmore.LoadMoreHandler;
import in.srain.cube.views.ptr.loadmore.LoadMoreListViewContainer;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 商家评价页面
 */
public class BusinessPinJiaFgt extends BaseFgt {


    @Bind(R.id.lv_data)
    ListView mLvData;

    @Bind(R.id.load_more_list_view_container)
    LoadMoreListViewContainer loadMoreListViewContainer;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.ptr_frame)
    PtrFrameLayout ptrFrame;

    private String merchant_id;
    private int p=1;
    private PinJiaAdapter adapter;
    private boolean isLoadMoreFinish=true;
    @Override
    public int getLayoutId() {
        return R.layout.index_business_pinjia_layout;
    }

    @Override
    public void initData() {
        merchant_id=getActivity().getIntent().getExtras().getString("merchant_id");

        PtrInitHelper.initPtr(getActivity(), ptrFrame);
        ptrFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {

                p=1;
                loadMoreListViewContainer.loadMoreFinish(false,true);
                doHttp(RetrofitUtils.createApi(Merchant.class).merchantCommentList(merchant_id, UserManger.getM_id(),"1"),1);


            }
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mLvData, header);
            }
        });

        loadMoreListViewContainer.useDefaultFooter();
        loadMoreListViewContainer.setAutoLoadMore(true);
        loadMoreListViewContainer.loadMoreFinish(false, true);

        loadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(final LoadMoreContainer loadMoreContainer){
                if (isLoadMoreFinish){
                    isLoadMoreFinish=false;
                    doHttp(RetrofitUtils.createApi(Merchant.class).merchantCommentList(merchant_id, UserManger.getM_id(),p+""),2);
                }

            }
        });


    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showLoadingContentDialog();
        isShowToast=false;
    }

    @Override
    public boolean setIsInitRequestData() {
        return true;
    }

    @Override
    public void requestData() {

        doHttp(RetrofitUtils.createApi(Merchant.class).merchantCommentList(merchant_id, UserManger.getM_id(),"1"),1);

    }


    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {

       switch(what){
           case 1:
               List<PinJiaInfo> list= AppJsonUtil.getArrayList(result,"comment_list",PinJiaInfo.class);
               ptrFrame.refreshComplete();


                  if (adapter==null){
                     adapter=new PinJiaAdapter(getActivity(),list,R.layout.index_business_pinjia_item);
                       mLvData.setAdapter(adapter);
                  }else {
                      adapter.removeAll();
                      adapter.addAll(list);
                  }

                   if (list.size()>0){
                       loadMoreListViewContainer.loadMoreFinish(false,true);
                       loadMoreListViewContainer.setVisibility(View.VISIBLE);
                       llEmpty.setVisibility(View.GONE);

                       p++;
                   }else {

                       loadMoreListViewContainer.loadMoreFinish(false,false);
                       loadMoreListViewContainer.setVisibility(View.GONE);
                       llEmpty.setVisibility(View.VISIBLE);
                   }

               break;
           case 2:

               List<PinJiaInfo> infoList= AppJsonUtil.getArrayList(result,"comment_list",PinJiaInfo.class);

               if (infoList.size()>0){
                   loadMoreListViewContainer.loadMoreFinish(false,true);
                   adapter.addAll(infoList);
                   p++;
               }else {
                   loadMoreListViewContainer.loadMoreFinish(false,false);
               }
               isLoadMoreFinish=true;
               break;
       }


        super.onSuccess(result, call, response, what);
    }


    @Override
    public void onFailure(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        switch(what){
            case 1:
                ptrFrame.refreshComplete();
                loadMoreListViewContainer.loadMoreFinish(false,false);
                loadMoreListViewContainer.setVisibility(View.GONE);
                llEmpty.setVisibility(View.VISIBLE);
                break;
            case 2:
                loadMoreListViewContainer.loadMoreFinish(false,false);
                isLoadMoreFinish=true;
                break;
        }

        super.onFailure(result, call, response, what);
    }

    @Override
    public void onError(Call<ResponseBody> call, Throwable t, int what) {
         ptrFrame.refreshComplete();
        loadMoreListViewContainer.loadMoreFinish(false,true);
        isLoadMoreFinish=true;
        super.onError(call, t, what);
    }
}
